package com.huluobo.a2109mouthdemo.addGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.huluobo.a2109mouthdemo.R;
import com.huluobo.a2109mouthdemo.adapter.AddGroupAdapter;
import com.huluobo.a2109mouthdemo.base.BaseActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;

import java.util.ArrayList;
import java.util.List;

public class AddGroupActivity extends BaseActivity<AddGroupContract.View, AddGroupPresenter> implements AddGroupContract.View {
    private EditText groupName, groupDesc;
    private Button createGroup, findGroup;

    private RecyclerView recyclerView;
    private AddGroupAdapter adapter;
    private List<EMGroupInfo> groupInfoList = new ArrayList<>();

    private String groupId;

    @Override
    public void onCreateGroupSuccess() {
        runOnUiThread(() -> {
            Toast.makeText(this, "创建群成功", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void onCreateGroupFailed(String error) {
        runOnUiThread(() -> {
            Toast.makeText(this, "创建群失败:" + error, Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void findGroupSuccess(List<EMGroupInfo> list) {
        runOnUiThread(() -> {
            groupInfoList.clear();
            groupInfoList.addAll(list);
            adapter.notifyDataSetChanged();
        });
    }

    @Override
    public void findGroupFailed(String error) {

    }

    @Override
    public void onJoinGroupSuccess() {
        runOnUiThread(() -> {
            Toast.makeText(this, "加群成功", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void onJoinGroupFailed(String error, int code) {
        runOnUiThread(() -> {
            if (code == 601) {
                Toast.makeText(this, "已经加入过该群,不能重复加入", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "加群失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void initData() {
        presenter = new AddGroupPresenter();
    }

    @Override
    protected void initView() {
        groupName = findViewById(R.id.addGroup_et_groupName);
        groupDesc = findViewById(R.id.addGroup_et_desc);
        createGroup = findViewById(R.id.addGroup_createGroup);

        createGroup.setOnClickListener(v -> {
            EMGroupOptions options = new EMGroupOptions();
            options.maxUsers = 100;
            options.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
            presenter.createGroup(groupName.getText().toString(), groupDesc.getText().toString(),
                    new String[]{EMClient.getInstance().getCurrentUser()}, "这是创建群", options);
        });
        findGroup = findViewById(R.id.addGroup_findGroup);
        findGroup.setOnClickListener(v -> {
            presenter.findGroup(10, null);
        });

        recyclerView = findViewById(R.id.addGroup_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddGroupAdapter(groupInfoList);
        recyclerView.setAdapter(adapter);

        adapter.setOnAddClick(groupId -> {
            presenter.joinGroup(groupId);
            this.groupId = groupId;
        });
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_add_group;
    }
}